package pro.shuangxi.common.controller;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * @author ：mengshx
 * @date ：Created in 2021/4/12 19:41
 * @description：图片服务器
 */
@Controller
public class PictureController {
    @RequestMapping("getPicture.do")
    public void getPicture(HttpServletRequest request, HttpServletResponse response,String path) {
        if (path.contains("jpg") || path.contains("png") || path.contains("jpeg")) {
            try {
                Image img = ImageIO.read(new File(path));
                BufferedImage tag = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
                // Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的优先级比速度高 生成的图片质量比较好 但速度慢
                tag.getGraphics().drawImage(img.getScaledInstance(100, 100, Image.SCALE_SMOOTH), 0, 0, null);
                ImageIO.write(tag, "jpeg", response.getOutputStream()); // 这里也可以传 FileOutputStream 写进文件里
            } catch (IOException e) {
                e.printStackTrace();
            }
//            File file = new File(path);
//            try(
//                FileInputStream fileInputStream = new FileInputStream(file);
//                ServletOutputStream outputStream = response.getOutputStream()
//            ) {
//                response.setContentType("image/jpeg");
//                int available = fileInputStream.available();
//                byte[] data = new byte[available];
//                fileInputStream.read(data);
//                outputStream.write(data);
//                outputStream.flush();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }

        }


    }


    public  void reduceImg(String imgsrc,OutputStream out) {
        try {
            File srcfile = new File(imgsrc);
            // 检查图片文件是否存在
            if (!srcfile.exists()) {
                System.out.println("文件不存在");
            }
            int[] results = new int[]{100,100};

            int widthdist = results[0];
            int heightdist = results[1];
            // 开始读取文件并进行压缩
            Image src = ImageIO.read(srcfile);

            // 构造一个类型为预定义图像类型之一的 BufferedImage
            BufferedImage tag = new BufferedImage( widthdist,  heightdist, BufferedImage.TYPE_INT_RGB);

            // 这边是压缩的模式设置
            tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist, Image.SCALE_SMOOTH), 0, 0, null);

            //将图片按JPEG压缩，保存到out中
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            encoder.encode(tag);
            //关闭文件输出流
            out.close();
        } catch (Exception ef) {
            ef.printStackTrace();
        }
    }

}
